package com.renwei.yunlong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.global.IntentKey;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 办公电话页面
 * <p>
 * Created by Administrator on 2017/8/23.
 */

public class EditCompanyInfoActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.et_company)
    EditText etCompany;
    private int type = 100;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_edit_compnay_info);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        initData();
    }


    private void initData() {
        Intent recIntent = getIntent();
        String title = recIntent.getExtras().getString(IntentKey.TITLE, "");
        if (StringUtils.value(title).length()==0) {
            title = "企业信息";
        }
        tvTitle.setText(title);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存");
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvRight.setOnClickListener(this);
        String recData = "";
        type = recIntent.getIntExtra(IntentKey.COMPANY_INFO_TYPE, 100);
        switch (type) {
            case 1:
                recData = recIntent.getStringExtra(IntentKey.COMPANY_NAME);
                etCompany.setInputType(InputType.TYPE_CLASS_TEXT);
                etCompany.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
                break;
            case 2:
                recData = recIntent.getStringExtra(IntentKey.OFFICE_EMAIL);
                etCompany.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                etCompany.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
                break;
            case 3:
                recData = recIntent.getStringExtra(IntentKey.COMPANY_LINK_MAN);
                etCompany.setInputType(InputType.TYPE_CLASS_TEXT);
                etCompany.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
                break;
            case 4:
                recData = recIntent.getStringExtra(IntentKey.COMPANY_LINK_PHONE);
                etCompany.setInputType(InputType.TYPE_CLASS_PHONE);
                etCompany.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
                break;
            case 5:
                recData = recIntent.getStringExtra(IntentKey.COMPANY_CARD_NUM);
                etCompany.setHint(Html.fromHtml("证件号码长度为20、输入字母/数字"));
                etCompany.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20), AppHelper.emojiFilter});
                break;
            case 6:
                etCompany.setInputType(InputType.TYPE_CLASS_NUMBER);
                recData = recIntent.getStringExtra(IntentKey.COMPANY_YEARS);
                break;
            default:
                break;
        }
        String temp = String.valueOf(recData);
        etCompany.setText(temp);
        etCompany.setSelection(etCompany.getText().toString().length());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                String temp = etCompany.getText().toString().trim();
                switch (type) {
                    case 1:
                        if (StringUtils.value(temp).length() == 0) {
                            showCenterInfoMsg("企业名称不能为空");
                            return;
                        }
                        setResult(IntentKey.COMPANY_NAME_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    case 2:
                        setResult(IntentKey.OFFICE_EMAIL_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    case 3:
                        setResult(IntentKey.COMPANY_LINK_MAN_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    case 4:
                        setResult(IntentKey.COMPANY_LINK_PHONE_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    case 5:
                        String reg = "^[A-Za-z0-9]+$"; // appear at least 24 times ^[0-9A-Za-z]{24,}
                        Pattern p = Pattern.compile(reg);
                        Matcher m = p.matcher(temp);
                        if (StringUtils.value(temp).length() == 0) {
                            showCenterInfoMsg("企业证件号码不能为空");
                            return;
                        } else if (!m.matches()) {
                            showCenterInfoMsg("证件号码格式不正确");
                            return;
                        }
                        setResult(IntentKey.COMPANY_CARD_NUM_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    case 6:
                        setResult(IntentKey.COMPANY_YEARS_KEY, new Intent().putExtra(IntentKey.CONTENT, temp));
                        break;
                    default:
                        break;
                }

                finish();
                break;
            default:
                break;
        }
    }


}
